package zzzhc.image.recognize.nn;

import org.joone.engine.FullSynapse;
import org.joone.engine.Layer;
import org.joone.engine.SigmoidLayer;
import org.joone.engine.learning.TeachingSynapse;
import org.joone.io.MemoryInputSynapse;
import org.joone.io.MemoryOutputSynapse;
import org.joone.net.NeuralNet;

public class SigmoidNN extends NNBase {
	private Layer hiddenLayer;

	private TeachingSynapse trainer;

	protected void buildNeuralNet() {
		inputLayer = new SigmoidLayer();
		hiddenLayer = new SigmoidLayer();
		outputLayer = new SigmoidLayer();
		inputLayer.setLayerName("input");
		hiddenLayer.setLayerName("hidden");
		outputLayer.setLayerName("output");

		// input -> hidden conn.
		FullSynapse synapse_IH = new FullSynapse();
		// hidden -> output conn.
		FullSynapse synapse_HO = new FullSynapse();

		synapse_IH.setName("IH");
		synapse_HO.setName("HO");

		// Connect the input layer with the hidden layer
		inputLayer.addOutputSynapse(synapse_IH);
		hiddenLayer.addInputSynapse(synapse_IH);

		// Connect the hidden layer with the output layer
		hiddenLayer.addOutputSynapse(synapse_HO);
		outputLayer.addInputSynapse(synapse_HO);

		trainer = new TeachingSynapse();

		// Connects the Teacher to the last layer of the net
		outputLayer.addOutputSynapse(trainer);

		neuralNet = new NeuralNet();
		neuralNet.addLayer(inputLayer, NeuralNet.INPUT_LAYER);
		neuralNet.addLayer(hiddenLayer, NeuralNet.HIDDEN_LAYER);
		neuralNet.addLayer(outputLayer, NeuralNet.OUTPUT_LAYER);
	}

	protected void doSetNNInput() {
		input = nnInput.createInputSynapse();
		inputLayer.removeAllInputs();
		inputLayer.addInputSynapse(input);

		if (output != null) {
			outputLayer.removeOutputSynapse(output);
		}
		output = new MemoryOutputSynapse();
		outputLayer.addOutputSynapse(output);

		inputLayer.setRows(nnInput.getFetureNum());
		hiddenLayer.setRows(nnInput.getFetureNum() + nnInput.getCodeTypeNum());
		outputLayer.setRows(nnInput.getCodeTypeNum());

		// Setting of the file containing the desired responses,
		// provided by a FileInputSynapse
		MemoryInputSynapse memInpTeach = nnInput.createDesireSynapse();
		trainer.setDesired(memInpTeach);

	}

}
